import { ShoppingWay } from "../../core/enum";
import { Caculator } from "../../models/caculator"
import { Cart } from "../../models/cart"
import { spuPaging } from "../../models/spu-paging";
// pages/cart/cart.js
const cart = new Cart()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cartItems: [],
        isEmpty: false,
        allChecked: false,
        totalPrice: 0,
        totalSkuCount: 0,
        spuPaging: null,
        loadingType: "loading"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        const cartData = cart.getAllSkuFromServer()
        if (cartData) {
            this.setData({
                cartItems: cartData.items
            })
        }

        this.initBottomSpuList()
    },
    async initBottomSpuList() {
        const paging = spuPaging.getLatestPaging()
        this.data.spuPaging = paging
        const data = await paging.getMoreData()

        if (!data) {
            return
        }
        //累加
        wx.lin.renderWaterFlow(data.items);
    },

    /**
      * 页面上拉触底事件的处理函数
      */
    onReachBottom: async function () {
        const data = await this.data.spuPaging.getMoreData();
        if (!data) {
            return
        }
        wx.lin.renderWaterFlow(data.items)
        if (!data.moreData) {
            this.setData({
                loadingType: 'end'
            })

        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const cart = new Cart()
        const cartItems = cart.getAllCartItemFromLocal().items
        if (cart.isEmpty()) {
            this.empty()
            return
        }
        this.setData({
            cartItems
        })
        this.notEmpty()
        this.isAllChecked()
        this.refreshCartData()
    },

    refreshCartData() {
        const checkedItems = cart.getCheckItems()
        const calcuator = new Caculator(checkedItems)
        calcuator.calc()
        this.setCalcData(calcuator)
    },

    setCalcData(calcuator) {
        const totalPrice = calcuator.getTotalPrice()
        const totalSkuCount = calcuator.getTotalSkuCount()
        this.setData({
            totalPrice,
            totalSkuCount
        })
    },


    onDeleteItem(event) {
        this.isAllChecked()
        this.refreshCartData()
    },
    onSingleCheck(event) {
        this.isAllChecked()
        this.refreshCartData()
    },
    onCheckAll(event) {
        const checked = event.detail.checked
        cart.checkAll(checked)
        this.setData({
            //引用类型
            cartItems: this.data.cartItems
        })
        this.refreshCartData()

    },
    isAllChecked() {
        const allChecked = cart.isAllChecked()
        this.setData({
            allChecked
        })
    },

    empty() {
        this.setData({
            isEmpty: true
        })
        wx.hideTabBarRedDot({
            index: 2,
        })
    },
    notEmpty() {
        this.setData({
            isEmpty: false
        })
        wx.showTabBarRedDot({
            index: 2,
        })
    },
    onCountFloat(event) {
        this.refreshCartData()
    },

    onSettle(event) {
        // 至少有一个订单
        if (this.data.totalSkuCount === 0) {
            return
        }
        wx.navigateTo({
            url: `/pages/order/order?way=${ShoppingWay.CART}`,

        });
    }



})